package cn.silver.framework.core.utils;

import cn.hutool.core.util.IdUtil;
import cn.silver.framework.core.exception.ServiceException;
import cn.silver.framework.core.utils.file.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@Component
public class CommonUtils {

    /**
     * 判断文件名是否带盘符，重新处理
     *
     * @param fileName
     * @return
     */
    public static String getFileName(String fileName) {
        //判断是否带有盘符信息
        // Check for Unix-style path
        int unixSep = fileName.lastIndexOf('/');
        // Check for Windows-style path
        int winSep = fileName.lastIndexOf('\\');
        // Cut off at latest possible point
        int pos = (winSep > unixSep ? winSep : unixSep);
        if (pos != -1) {
            // Any sort of path separator found...
            fileName = fileName.substring(pos + 1);
        }
        //替换上传文件名字的特殊字符
        fileName = fileName.replace("=", "").replace(",", "").replace("&", "").replace("#", "");
        //替换上传文件名字中的空格
        fileName = fileName.replaceAll("\\s", "");
        return fileName;
    }


    /**
     * 本地文件上传
     *
     * @param mf      文件
     * @param bizPath 自定义路径
     * @return
     */
    public static String uploadLocal(MultipartFile mf, String bizPath, String uploadpath) {
        try {
            File file = new File(uploadpath + File.separator + bizPath + File.separator);
            if (!file.exists()) {
                file.mkdirs();// 创建文件根目录
            }
            String filename = mf.getOriginalFilename();// 获取文件名
            filename = CommonUtils.getFileName(filename);
            String fileName = IdUtil.getSnowflakeNextIdStr() + filename.substring(filename.indexOf("."));
            String savePath = file.getPath() + File.separator + fileName;
            File savefile = new File(savePath);
            FileCopyUtils.copy(mf.getBytes(), savefile);
            String dbpath = null;
            if (StringUtils.isNotBlank(bizPath)) {
                dbpath = bizPath + File.separator + fileName;
            } else {
                dbpath = fileName;
            }
            if (dbpath.contains("\\")) {
                dbpath = dbpath.replace("\\", "/");
            }
            return dbpath;
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return "";
    }

    public static String moveFile(String oldPath, String newPath) {
        File source = new File(oldPath);
        File target = new File(newPath);
        if (!source.exists()) {
            throw new ServiceException("源文件不存在！");
        }
        if (!target.getParentFile().exists()) {
            target.getParentFile().mkdirs();
        }
        FileUtils.move(source, target, true);
        if (newPath.contains("\\")) {
            newPath = newPath.replace("\\", "/");
        }
        return newPath;
    }

}
